package com.rentplant.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rentplant.Pojo.order;
import com.rentplant.Pojo.orderanditem;
import com.rentplant.Pojo.outrentuserinfo;
import com.rentplant.service.impl.orderServiceImp;
import org.junit.platform.commons.function.Try;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLOutput;
import java.util.List;

@RestController//让它知道这是controller
@RequestMapping("/order")
public class orderController {

    @Autowired
    private orderServiceImp orderserviceimp;

//    付款生成待付款订单
    @RequestMapping("payOrder")
    public boolean payOrder(@RequestBody JSONObject req){
        try {
//            System.out.println(req);
            return orderserviceimp.payOrder(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    未付款生成待付款订单
    @RequestMapping("/noPayOrder")
    public boolean noPayOrder(@RequestBody JSONObject req){
        try{
            return orderserviceimp.noPayOrder(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }


    //    查找用户全部订单
    @RequestMapping("/selectAllOrderById")
    public List<orderanditem> selectAllOrderById(@RequestBody JSONObject req){
        try {
            return orderserviceimp.selectAllOrderById(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    查找该用户的未付款订单
    @RequestMapping("/selectOrderNoPay")
    public List<order> selectOrderNoPay(@RequestBody JSONObject req){
        try {
            return orderserviceimp.selectOrderNoPay(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

    //    查找该用户的待收货订单
    @RequestMapping("/selectOrderWaitItem")
    public List<order> selectOrderWaitItem(@RequestBody JSONObject req){
        try {
            return orderserviceimp.selectOrderWaitItem(req);
        }catch (Exception e){
            throw  new RuntimeException(e);
        }
    }

    //    查找租用中订单
    @RequestMapping("/selectOrderInRent")
    public List<order> selectOrderInRent(@RequestBody JSONObject req){
        try{
            return orderserviceimp.selectOrderInRent(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    查找待归还订单
    @RequestMapping("/selectOrderBack")
    public  List<order> selectOrderBack(@RequestBody JSONObject req){
        try {
            return orderserviceimp.selectOrderBack(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //查找已完成和已评价订单
    @RequestMapping("/selectOrderFinish")
    public  List<order> selectOrderFinish(@RequestBody JSONObject req){
        try {
            return orderserviceimp.selectOrderFinish(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }



    //    查找待付款订单
    @RequestMapping("/userOrderNoPay")
    public List<orderanditem> userOrderNoPay(@RequestBody JSONObject req){
        try {
            return orderserviceimp.userOrderNoPay(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    查找待发货订单
    @RequestMapping("/userOrderWaitItem")
    public List<orderanditem> userOrderWaitItem(@RequestBody JSONObject req){
        try {
            return orderserviceimp.userOrderWaitItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    //查找自己出售物品的订单
    @RequestMapping("/otherOrderWaitItem")
    public List<orderanditem> otherOrderWaitItem(@RequestBody JSONObject req){
        try {
            return orderserviceimp.otherOrderWaitItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


    //    查找租用中订单
    @RequestMapping("/userOrderInRent")
    public List<orderanditem> userOrderInRent(@RequestBody JSONObject req){
        try {
            return orderserviceimp.userOrderInRent(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    查找待归还订单
    @RequestMapping("/userOrderBack")
    public List<orderanditem> userOrderBack(@RequestBody JSONObject req){
        try {
            return orderserviceimp.userOrderBack(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    查找已完成订单
    @RequestMapping("/userOrderFinish")
    public  List<orderanditem> userOrderFinish(@RequestBody JSONObject req){
        try {
//            System.out.println(req);
            return orderserviceimp.userOrderFinish(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }



    //    按照orderid查询订单
    @RequestMapping("/orderById")
    public  List<orderanditem> orderById(@RequestBody JSONObject req){
        try {
            return orderserviceimp.orderById(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


    //    确认付款修改订单为待发货
    @RequestMapping("/updateToWaitItem")
    public boolean updateToWaitItem(@RequestBody JSONObject req){
        try{
            return orderserviceimp.updateToWaitItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //用户删除未付款的
    @RequestMapping("/delNoPay")
    public boolean delNoPay(@RequestBody JSONObject req){
        try{
            return orderserviceimp.delNoPay(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    // 提交交易快照完成交货
    @RequestMapping("/updateToInRent")
    public boolean updateToInRent(@RequestBody JSONObject req){
        try {
            return orderserviceimp.updateToInRent(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    提交租赁中快照
    @RequestMapping("/updateTransactionPic")
    public boolean updatePic(@RequestBody JSONObject req){
        try {
            return orderserviceimp.updatePic(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //续租修改订单信息
    @RequestMapping("/rentnew")
    public boolean rentnew(@RequestBody JSONObject req){
        try {
            return orderserviceimp.rentnew(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //删除快照
    @RequestMapping("/delRentPic")
    public boolean delRentPic(@RequestBody JSONObject req){
        try {
            return orderserviceimp.delRentPic(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //归还物品
    @RequestMapping("/backItemTime")
    public boolean backItemTime(@RequestBody JSONObject req){
        try {
            return orderserviceimp.backItemTime(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    //    归还完毕将物品下架
    @RequestMapping("/itemtodel")
    public boolean itemtodel(@RequestBody JSONObject req){
        try {
            return orderserviceimp.itemtodel(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    完成订单付款的对象
    @RequestMapping("/outRentUserInfo")
    public List<outrentuserinfo>outRentUserInfo(@RequestBody JSONObject req){
        try {
            return orderserviceimp.outRentUserInfo(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


    //    订单已评价
    @RequestMapping("/orderForJudge")
    public boolean orderForJudge(@RequestBody JSONObject req){
        try {
            String orderid = req.getString("orderid");
            return orderserviceimp.orderForJudge(orderid);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //查找用户物品出租订单
    @RequestMapping("/mineRent")
    public List<orderanditem> mineRent(@RequestBody JSONObject req){
        try {
//            String openid = req.getString("openid");
            return orderserviceimp.mineRent(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    管理订单
    //    查询全部分页订单
    @RequestMapping("/allOrder")
    public List<order> allOrder(@RequestBody JSONObject req){
        try {
            return orderserviceimp.allOrder(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    查询全部订单
    @RequestMapping("/allOrders")
    public  List<order> allOrders(){
        try {
            return orderserviceimp.allOrders();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //修改订单信息
    @RequestMapping("/updateOrder")
    public boolean updateOrder(@RequestBody JSONObject req){
        try {
            return orderserviceimp.updateOrder(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    条件查找
    @RequestMapping("/searchOrder")
    public List<order> searchOrder(@RequestBody JSONObject req){
        try {
            return orderserviceimp.searchOrder(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    条件查找
    @RequestMapping("/searchOrders")
    public List<order> searchOrders(@RequestBody JSONObject req){
        try {
            return orderserviceimp.searchOrders(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

}
